package com.gams.springboot.controller;

import com.gams.springboot.pojo.Role;
import com.gams.springboot.pojo.User;
import com.gams.springboot.services.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;

/**
 * @author tql
 * @current springboot
 * @create 2021-09-9-10:10
 */
@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/roleList")
    public ModelAndView queryRole(){
         ModelAndView v = new ModelAndView("/page/table/add.html");
         v.addObject("roleList", roleService.selectAllRole());
        return v;
    }



    @RequestMapping(value = "/role",method = RequestMethod.GET )
    @ResponseBody
    public Object queryUser(@RequestParam(value="page") int page,
                            @RequestParam(value="limit") int limit){
        HashMap<String, Object> map = new HashMap<>();
        PageInfo pageInfo = roleService.findAllUser(page,limit);
        map.put("code",0);
        map.put("msg","加载成功");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }
    @RequestMapping("/addRole1")
    public ModelAndView add(){
        ModelAndView v = new ModelAndView("/page/table/add1.html");
        return v;
    }
    @PostMapping("/addRole")
    @ResponseBody
    public long addRole(@RequestBody Role role,
                        @RequestParam(value = "list") List<Long> list){
        return roleService.addRole(role,list);
    }

    @RequestMapping("/edit1")
    public ModelAndView edit(@RequestParam(value = "id")int id,
                             @RequestParam(value = "name")String name,
                             @RequestParam(value = "status")String status
    ){
        ModelAndView v = new ModelAndView("/page/table/edit1.html");
        v.addObject("id" ,id);
        v.addObject("name",name);
        v.addObject("status",status);
        return v;
    }

    @PostMapping("/updateRole")
    @ResponseBody
    public int updateUser(@RequestBody  Role role,
                          @RequestParam(value="id") String id,
                          @RequestParam(value ="list") List<Long> list){

        return roleService.updateRole(role,id,list);
    }

    @PostMapping("/deleteRole")
    @ResponseBody
    public int deleteUser(String id){
        long id1 = Integer.parseInt(id);
        int result = roleService.deleteRole(id1);
        return result;
    }
}
